package com.neoCalf.neoChat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neoCalf.neoChat.mapper.ChatMessageMapper;
import com.neoCalf.neoChat.mapper.ChatSessionMapper;
import com.neoCalf.neoChat.mapper.SessionMapper;
import com.neoCalf.neoChat.service.ChatService;
import com.neoCalf.neoCommon.entity.*;
import com.neoCalf.neoCommon.utils.DataTool;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author 小牛
 * @version 1.0
 * @description TODO
 * @date 2025/4/13 2:11
 */
@Service(value = "chatService")
public class ChatServiceImpl extends ServiceImpl<SessionMapper, Session> implements ChatService {
    @Resource
    private SessionMapper sessionMapper;
    @Resource
    private ChatSessionMapper chatSessionMapper;
    @Resource
    private ChatMessageMapper chatMessageMapper;

    @Override
    public void addSession(Session session) {
        int insert = sessionMapper.insert(session);
        DataTool.checkAffectRow(insert, "添加会话失败");
    }

    @Override
    public void addSessionList(List<Session> sessionList) {
        saveBatch(sessionList);
    }

    @Override
    public void addChatSession(ChatSession chatSession) {
        int insert = chatSessionMapper.insert(chatSession);
        DataTool.checkAffectRow(insert, "添加聊天会话失败");
    }

    @Override
    public void addChatMessage(ChatMessage chatMessage) {
        int insert = chatMessageMapper.insert(chatMessage);
        DataTool.checkAffectRow(insert, "添加聊天消息失败");
    }

    @Override
    public List<Session> findSessionList(String userId) {
        LambdaQueryWrapper<Session> sessionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sessionLambdaQueryWrapper.eq(Session::getUserId, userId);
        List<Session> sessionList = sessionMapper.selectList(sessionLambdaQueryWrapper);
        if (!sessionList.isEmpty()) {
            for (Session session : sessionList) {
                LambdaQueryWrapper<ChatSession> chatSessionLambdaQueryWrapper = new LambdaQueryWrapper<>();
                chatSessionLambdaQueryWrapper.eq(ChatSession::getSessionId, session.getSessionId());
                ChatSession chatSession = chatSessionMapper.selectOne(chatSessionLambdaQueryWrapper);
                session.setChatSession(chatSession);
            }
        }
        return sessionList;
    }

    @Override
    public List<ChatMessage> findChatMessageList(List<String> list) {
        LambdaQueryWrapper<ChatMessage> chatSessionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        chatSessionLambdaQueryWrapper.in(ChatMessage::getSessionId, list);
        chatSessionLambdaQueryWrapper.orderByAsc(ChatMessage::getSendTime);
        return chatMessageMapper.selectList(chatSessionLambdaQueryWrapper);
    }

    @Override
    public void delContactSession(String sessionId) {
        LambdaQueryWrapper<Session> sessionWrapper = new LambdaQueryWrapper<>();
        sessionWrapper.eq(Session::getSessionId, sessionId);
        sessionMapper.delete(sessionWrapper);
        LambdaQueryWrapper<ChatSession> chatSessionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        chatSessionLambdaQueryWrapper.eq(ChatSession::getSessionId, sessionId);
        chatSessionMapper.delete(chatSessionLambdaQueryWrapper);
        LambdaQueryWrapper<ChatMessage> chatMessageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        chatMessageLambdaQueryWrapper.eq(ChatMessage::getSessionId, sessionId);
        chatMessageMapper.delete(chatMessageLambdaQueryWrapper);
    }

    @Override
    public void updateSession(Session session) {
        LambdaUpdateWrapper<Session> sessionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        sessionLambdaUpdateWrapper.eq(Session::getSessionId, session.getSessionId());
        sessionLambdaUpdateWrapper.set(DataTool.isStrLegal(session.getContactName()), Session::getContactName, session.getContactName());
        sessionMapper.update(null, sessionLambdaUpdateWrapper);
    }

    @Override
    public void delGroupSessionBatch(String[] list, String groupId) {
        LambdaQueryWrapper<Session> sessionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sessionLambdaQueryWrapper.eq(Session::getContactId, groupId);
        sessionLambdaQueryWrapper.in(Session::getUserId, (Object) list);
        sessionMapper.delete(sessionLambdaQueryWrapper);
    }

    @Override
    public void updateChatSession(ChatSession chatSession) {
        LambdaUpdateWrapper<ChatSession> chatSessionLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        chatSessionLambdaUpdateWrapper.eq(ChatSession::getSessionId, chatSession.getSessionId());
        chatSessionMapper.update(chatSession, chatSessionLambdaUpdateWrapper);
    }
}
